import {
  View,
  Text,
  StyleSheet,
  Image,
  Pressable,
  StyleProp,
  ViewStyle,
  Animated,
  LayoutChangeEvent,
} from 'react-native'
import { CardDataInterface } from '../../assets/data/data'
import { useContext, useEffect, useRef, useState } from 'react'
import { NavigationContext } from '@react-navigation/native'
import { compose, flexCol } from '../../assets/style/common'
import OwlRadioBox from '../common/owlRadioBox'
import Price from '../price'
import { LinearGradient } from 'expo-linear-gradient'

interface GoodsCardProps {
  onTouchRadio: (id: number) => void
  isEdit?: boolean
  onErase?: (id: number) => void
  style?: StyleProp<ViewStyle>
}

export default function GoodsCard(props: CardDataInterface & GoodsCardProps) {
  const navigation = useContext(NavigationContext)
  const animateValue = useRef(new Animated.Value(0)).current
  const [height, setHeight] = useState(100)
  function goDetail() {
    navigation?.navigate('GoodDetail')
  }
  function onLayout(e: LayoutChangeEvent) {
    setHeight(e.nativeEvent.layout.height)
  }
  useEffect(() => {
    Animated.timing(animateValue, {
      toValue: props.isEdit ? -48 : 0,
      duration: 300,
      useNativeDriver: false,
    }).start()
  }, [props.isEdit])
  return (
    <View
      style={compose(flexCol.default, cardStyles.goodCnt, props.style)}
      onLayout={onLayout}
    >
      <View style={cardStyles.menuWrap}>
        <OwlRadioBox
          choosed={props.choosed}
          style={cardStyles.leftMenu}
          onPress={() => props.onTouchRadio(props.id)}
        />
      </View>
      <Pressable onPress={goDetail}>
        <Image source={props.cover} style={cardStyles.cover} />
      </Pressable>
      <Pressable style={cardStyles.cnt} onPress={goDetail}>
        <Text numberOfLines={1} style={cardStyles.tit}>
          {props.title}
        </Text>
        <View style={flexCol(cardStyles.typeWrap)}>
          <Text style={cardStyles.type}>{props.type}</Text>
          <Text style={cardStyles.typeArrow}>&#xe643;</Text>
        </View>
        <View style={flexCol(cardStyles.attrs)}>
          <Text style={compose(cardStyles.att, cardStyles.attRed)}>
            6期免期
          </Text>
          <Text style={compose(cardStyles.att, cardStyles.attRed)}>
            库存不足
          </Text>
        </View>
        <View style={flexCol(cardStyles.attrs)}>
          <Text style={cardStyles.att}>7天无理由退货</Text>
          <Text style={cardStyles.att}>极速退款</Text>
        </View>
        <Price price={props.price + ''} fontSize={16} fontWeight />
      </Pressable>
      <View style={flexCol(cardStyles.gCount)}>
        <Text style={cardStyles.hline} />
        <Text style={cardStyles.vline} />
        <Text style={cardStyles.gCountN}>{props.count}</Text>
      </View>
      <Animated.View
        style={[
          cardStyles.eraseWrap,
          {
            height: height,
            transform: [{ translateX: animateValue }],
          },
        ]}
      >
        <LinearGradient
          start={{ x: 0, y: 0.5 }}
          end={{ x: 0.5, y: 0.5 }}
          style={[cardStyles.eraseBgm, flexCol.default]}
          colors={['#ffffff33', '#ffffff']}
        >
          <Pressable onPress={() => props.onErase && props.onErase(props.id)}>
            <Text style={cardStyles.eraseIcon}>&#xe617;</Text>
          </Pressable>
        </LinearGradient>
      </Animated.View>
    </View>
  )
}

const cardStyles = StyleSheet.create({
  goodCnt: {
    marginBottom: 12,
    padding: 12,
    paddingTop: 12,
    paddingBottom: 12,
    paddingLeft: 0,
    marginLeft: 10,
    marginRight: 10,
    borderRadius: 6,
    backgroundColor: '#fff',
    alignItems: 'flex-start',
    overflow: 'hidden',
  },
  cover: {
    width: 98,
    height: 98,
    borderRadius: 6,
    marginRight: 12,
    resizeMode: 'contain',
    backgroundColor: '#f1f1f1',
  },
  type: {
    color: '#999',
    fontSize: 14,
    borderRadius: 4,
  },
  typeWrap: {
    marginBottom: 8,
  },
  typeArrow: {
    fontSize: 12,
    fontFamily: 'iconfont',
    marginLeft: 4,
    color: '#999',
    transform: [{ rotate: '90deg' }],
  },
  attrs: {
    marginLeft: -6,
    marginRight: -6,
    alignItems: 'flex-start',
  },
  att: {
    borderWidth: 1,
    borderColor: '#f3bc3899',
    borderRadius: 2,
    fontSize: 10,
    color: '#f3bc38',
    marginBottom: 6,
    marginLeft: 6,
    paddingHorizontal: 2,
    marginRight: 6,
  },
  attRed: {
    borderColor: '#f392a699',
    color: '#f392a6',
  },
  cnt: {
    flex: 1,
    paddingRight: 6,
    height: '100%',
  },
  tit: {
    fontSize: 16,
    marginBottom: 4,
  },
  gCount: {
    minwidth: 24,
    height: 24,
    borderRadius: 4,
    borderWidth: 1,
    borderColor: '#ccc',
    color: '#333',
    alignSelf: 'flex-end',
    position: 'relative',
    paddingHorizontal: 2,
  },
  gCountN: {
    color: '#333',
    marginLeft: 12,
    fontSize: 14,
  },
  hline: {
    transform: [
      {
        rotateZ: '45deg',
      },
    ],
    width: 8,
    height: 1,
    backgroundColor: '#333',
    position: 'absolute',
    left: 4,
    bottom: 10,
  },
  vline: {
    transform: [
      {
        rotateZ: '-45deg',
      },
    ],
    width: 8,
    height: 1,
    position: 'absolute',
    left: 4,
    backgroundColor: '#333',
    bottom: 10,
  },
  menuWrap: {
    position: 'relative',
    display: 'flex',
    alignSelf: 'center',
    justifyContent: 'center',
    width: 42,
  },
  leftMenu: {
    left: 0,
    position: 'absolute',
    width: '100%',
  },
  leftMenuClose: {
    width: 18,
    height: 18,
  },
  eraseWrap: {
    position: 'absolute',
    top: 0,
    right: -48,
    height: '100%',
    width: 48,
    zIndex: 10,
  },
  eraseBgm: {
    width: '100%',
    height: '100%',
    justifyContent: 'center',
  },
  eraseIcon: {
    fontSize: 16,
    fontFamily: 'iconfont',
    color: '#ff2222',
  },
})
